'use strict'

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    const { STRING, DATE, INTEGER, TEXT } = Sequelize
    await queryInterface.createTable('comment', {
      id: {
        type: INTEGER(20).UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
        comment: '主键id'
      },
      content: {
        type: TEXT,
        allowNull: false,
        comment: '评论内容'
      },
      article_id: {
        type: INTEGER(20).UNSIGNED,
        allowNull: false,
        comment: '文章id',
        references: {
          model: 'article',
          key: 'id'
        },
        onUpdate: 'restrict', // 更新时操作
        onDelete: 'cascade' // 删除时操作
      },
      username: {
        type: STRING(20),
        allowNull: false,
        comment: '评论用户'
      },
      avatar: {
        type: STRING(255),
        allowNull: false,
        comment: '评论用户头像'
      },
      created_at: DATE,
      updated_at: DATE
    })
  },

  async down(queryInterface) {
    queryInterface.dropTable('comment')
  }
}
